<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 我的会员 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.name" placeholder="会员名" class="handle-input mr10" @keyup.enter.native="handleSearch"></el-input>
                <el-input
                    v-model="query.cardId"
                    placeholder="卡号/手机号"
                    class="handle-input mr10"
                    @keyup.enter.native="handleSearch"
                ></el-input>
                <el-select v-model="query.categoryId" clearable placeholder="-选择会员等级-">
                    <el-option v-for="item in selectMenberLevel" :key="item.id" :label="item.title" :value="item.id"> </el-option>
                </el-select>
                &nbsp;
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <!-- <el-button type="primary" icon="el-icon-plus" @click="handleEdit()">新增</el-button> -->
            </div>
            <el-table
                v-loading="loading"
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                row-key="id"
            >
                @selection-change="handleSelectionChange" >
                <el-table-column prop="wechatName" label="微信名"></el-table-column>
                <el-table-column class-name="user2" prop="fullName" label="姓名"></el-table-column>
                <el-table-column prop="cardID" label="卡号" align="center"></el-table-column>
                <el-table-column prop="mobile" label="手机号" align="center"></el-table-column>
                <el-table-column prop="memberLevel" label="会员等级" align="center"></el-table-column>
                <el-table-column prop="storeName" label="所属门店" align="center"></el-table-column>
                <el-table-column prop="sex" :formatter="sexFilter" label="性别" align="center"></el-table-column>
                <el-table-column prop="integral" label="可用积分" align="center"></el-table-column>
                <el-table-column prop="balance" label="可用余额" align="center"></el-table-column>
                <el-table-column prop="regSource" :formatter="sourceFilter" label="注册来源" align="center"></el-table-column>
                <el-table-column prop="regTime" label="注册时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope" v-if="scope.row.id > 0">
                        <el-button type="text" icon="el-icon-view" @click="$router.push({ path: '/membercore', query: { id: scope.row.id } })">明细</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    @size-change="handleSizeChange"
                    background
                    @current-change="handleCurrentChange"
                    :current-page="1"
                    :page-sizes="[10, 15, 20, 30]"
                    :page-size="10"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageTotal"
                >
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog :before-close="closeDialog" :close-on-click-modal="false" title="系统管理员" :visible.sync="editVisible" width="40%">
            <el-form ref="form" :model="form" id="mform" label-width="100px" :rules="rulesForm">
                <el-form-item label="用户姓名" prop="name">
                    <el-input v-model="form.name" id="name"></el-input>
                </el-form-item>
                <el-form-item label="登录名" prop="loginName">
                    <el-input v-model="form.loginName" id="loginName"></el-input>
                </el-form-item>
                <el-form-item label="备注信息" prop="description">
                    <el-input id="description" type="textarea" :rows="3" placeholder="不超过50个字" v-model="form.description"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeDialog()">取 消</el-button>
                <el-button type="primary" @click="saveEdit('form')">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import axios from 'axios';
export default {
    name: 'user_list',
    data() {
        return {
            query: {
                name: '',
                cardId: '',
                categoryId: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            editVisible: false,
            selectMenberLevel: [],
            pageTotal: 0,
            form: {},
            loading: false,
            rulesForm: {}
        };
    },
    created() {
        this.loading = true;
        this.getSelect();
        this.getData();
    },
    methods: {
        sexFilter(row, column) {
            var str = '';
            switch (row.sex) {
                case 0:
                    str = '男';
                    break;
                case 1:
                    str = '女';
                    break;
                default:
                    str = row.sex;
                    break;
            }
            return str;
        },
        sourceFilter(row, column) {
            var str = '';
            switch (row.regSource) {
                case 0:
                    str = '微信注册';
                    break;
                case 1:
                    str = '小程序注册';
                    break;
                case 2:
                    str = '到店注册';
                    break;
                case 3:
                    str = '会员推荐';
                    break;
                default:
                    str = row.regSource;
                    break;
            }
            return str;
        },

        // 触发搜索按钮
        handleSearch() {
            this.query.pageIndex = 1;
            this.getData();
        },
        getData() {
            this.loading = true;
            var level = [];
            if (this.query.categoryId != '') {
                level.push(this.query.categoryId);
            }
            var parmas = {
                name: this.query.name,
                categoryId: level,
                cardId: this.query.cardId,
                pageIndex: this.query.pageIndex,
                pageSize: this.query.pageSize
            };

            axios({
                url: config.gatewayUrl + '/member/member/getpage',
                method: 'post',
                data: parmas,
                headers: {
                    // 'Content-Type': 'multipart/form-data'
                }
            }).then((res) => {
                this.pageTotal = res.data.total;
                this.tableData = res.data.list;
                this.loading = false;
            });
        },
        getSelect() {
            axios({
                url: config.gatewayUrl + '/member/MemberLevel/GetMemberLevelSelect',
                method: 'post',
                headers: {
                    // 'Content-Type': 'multipart/form-data'
                }
            }).then((res) => {
                this.selectMenberLevel = res.data;
            });
        },

        // 删除操作
        handleDelete(index, row) {
            this.$message.error('暂不允许删除!');

            // 二次确认删除
            // var that = this;
            // this.$confirm('确定要删除【' + row.name + '】吗？', '提示', {
            //     type: 'warning'
            // })
            //     .then(() => {
            //         axios.get(config.gatewayUrl + '/set/user/delete/' + row.id).then((res) => {
            //             that.getData();
            //             that.tableData.splice(index, 1);
            //             that.$message.success('删除成功');
            //         });
            //     })
            //     .catch(() => {});
        },

        // 编辑操作
        handleEdit(index, row) {
            if (row) {
                this.idx = index;

                this.form = row;
            }

            this.editVisible = true;

            // this.$refs.form.resetFields(); //移除表单校验并初始化表单，一般用于添加
            // this.$refs.form.clearValidate(); //移除表单校验，一般用于修改
        },
        // 保存编辑
        saveEdit(formName) {
            var that = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    var dto = this.form;

                    axios
                        .post(config.gatewayUrl + '/set/user/post', dto)
                        .then((res) => {
                            if (res.data.result == false) {
                                that.$message.error(res.data.promptMsg);
                                console.log(res);
                            } else {
                                that.$refs.form.clearValidate();
                                //that.editVisible = false;
                                that.closeDialog();
                                that.$message.success(`保存成功!`);
                                that.getData();
                            }
                        })
                        .catch((error) => {
                            console.log(error); //  错误处理 相当于error
                        });
                    that.$refs.form.clearValidate();
                } else {
                    return false;
                }
            });
        },

        closeDialog() {
            var that = this;
            //this.$refs.form.resetFields();
            this.form = {
                id: '',
                name: '',
                loginName: '',
                description: '',
                tenantId: ''
            };
            setTimeout(function () {
                that.$refs.form.clearValidate();
            }, 100);
            this.editVisible = false;
        },
        // 分页导航
        handlePageChange(val) {
            //跳转到
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        handleSizeChange(val) {
            //改变页码
            this.query.pageSize = val;
            this.getData();
        },
        handleCurrentChange(val) {
            //下一页console.log(`当前页: ${val}`);
            this.query.pageIndex = val;
            this.getData();
        }
    },
    watch: {}
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
